package cn.leyaber.hrm.exception;


import cn.leyaber.hrm.exception.handler.GlobalException;
import cn.leyaber.hrm.util.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.file.AccessDeniedException;

//拦截全局异常
@ControllerAdvice //打了这个标签的类可以在执行其他的controller之前/之后做执行
@ResponseBody
public class GlobalExceptionHandler {

    //@ExceptionHandler :处理异常:拦截异常 拦截自定义异常
    @ExceptionHandler(GlobalException.class)
    public AjaxResult exceptionHandler(GlobalException e ){
        return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
    }
    //@ExceptionHandler :处理异常:拦截异常 拦截自定义异常
    @ExceptionHandler(AccessDeniedException.class)
    public AjaxResult AccessDeniedException(AccessDeniedException e ){
        return AjaxResult.me().setSuccess(false).setMessage("没有访问权限");
    }
    //@ExceptionHandler :处理异常:拦截异常 拦截系统内部异常
    @ExceptionHandler(Exception.class)
    public AjaxResult exceptionHandler(Exception e ){
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage("系统异常啦，请联系管理员");
    }

}
